package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.exception.LoginException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference
    private IUserInfoRedisService userInfoRedisService;


    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkPhone(String phone) {
        return userInfoMapper.selectCountByPhone(phone) > 0;
    }

    @Override
    public void sendVerifyCode(String phone) {
        // 获取随机数作为验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0,4);

        StringBuilder sb = new StringBuilder();
        sb.append("您好,您的验证码为").append(code).append("请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟之内使用");
        System.out.println(sb);

        userInfoRedisService.addVerifyCode(phone, code);
    }

    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) throws LoginException {
        // 非空判断
        AssertUtil.hasLength(phone, "手机号码不能为空");
        AssertUtil.hasLength(nickname, "昵称不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        AssertUtil.hasLength(rpassword, "确认密码不能为空");
        AssertUtil.hasLength(verifyCode, "验证码不能为空");
        // 判断密码密码是否一致
        AssertUtil.isEquals(password, rpassword, "两次密码不一致");
        // 判断手机号码是否正确

        // 判断验证码是否一致
        String code = userInfoRedisService.getVerfiyCode(phone);
        if (code != null){
            if (!code.equalsIgnoreCase(verifyCode)){
                throw new LoginException("验证码错误或失效");
            }
        }

        // 用户注册
        UserInfo userInfo = new UserInfo();
        userInfo.setState(UserInfo.STATE_NORMAL);
        userInfo.setPassword(password);
        userInfo.setNickname(nickname);
        userInfo.setHeadImgUrl("/images/default.png");
        userInfo.setPhone(phone);
        userInfoMapper.insert(userInfo);
    }

    @Override
    public String userLogin(String username, String password) {
        // 1: 获取参数: username和password
        // 2: 作为sql参数查询userInfo对象
        AssertUtil.hasLength(username, "用户名不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        UserInfo userInfo = userInfoMapper.selectUserByUsernameAndPassword(username, password);

        // 3: 判断userInfo对象是否存在
        // 4: 如果不存在, 提示账号或密码错误
        if(userInfo == null){
            throw new LoginException("用户名或密码错误");
        }
        // 5: 如果存在, 表示登陆成功

        // 6: 使用UUID创建随机的令牌(token) String token = 创建出来的随机码
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        // 7: 将token的值作为redis的key, userInfo则为value值, 缓存到redis数据库中, 注意: 设置token有效时间是30风中
        userInfoRedisService.setToken(token, userInfo);

        return token;
        // 8: 创建cookie对象 Cookie c = new Cookie("token", 创建出来的随机码) 注意: 设置cookie存活时间30分钟
        // 9: 使用response对象将cookie对象传输到浏览器中

    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }
}
